package com.ebt.m.proposal_v2.ui.function;

import com.ebt.m.proposal_v2.bean.InsuranceEntity;
import com.ebt.m.proposal_v2.widget.view.InsuranceCellView;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于标识管理唯一InsuranceCellView的栈
 *
 * @author archer.qi
 *         Created on 2017/7/18.
 */
public class ViewStack {

    public static class ViewObject {
        public int id;
        public InsuranceCellView view;
        public InsuranceEntity data;

        public boolean isNull() {
            return id <= 0 || view == null || data == null;
        }
    }

    private List<ViewObject> mViewStack;

    public ViewStack() {
        mViewStack = new ArrayList<>();
    }

    public ViewObject getViewObject(int id) {
        if (mViewStack == null || mViewStack.size() == 0 || id <= 0) {
            return null;
        }
        for (ViewObject obj : mViewStack) {
            if (obj != null && obj.id == id) {
                return obj;
            }
        }
        return null;
    }

    public ViewObject put(int id, InsuranceCellView view, InsuranceEntity data) {
        if (view == null || id <= 0 || data == null) {
            return null;
        }
        ViewObject viewObject = new ViewObject();
        viewObject.id = id;
        viewObject.view = view;
        viewObject.data = data;
        mViewStack.add(viewObject);

        return viewObject;
    }

    public synchronized void remove(int id) {
        if (mViewStack == null || mViewStack.size() == 0) {
            return;
        }
        for (ViewObject obj : mViewStack) {
            if (obj != null && obj.id == id && mViewStack.contains(obj)) {
                mViewStack.remove(obj);
                return;
            }
        }
    }

    public boolean contains(int id) {
        if (mViewStack == null || mViewStack.size() == 0) {
            return false;
        }
        for (ViewObject obj : mViewStack) {
            if (obj != null && obj.id == id) {
                return true;
            }
        }
        return false;
    }

    public void clearAll() {
        if (mViewStack != null) {
            mViewStack.clear();
        }
    }

}
